<?php
/**
 * PHPTAL templating engine
 *
 * PHP Version 5
 *
 * @category HTML
 * @package  PHPTAL
 * @author   Laurent Bedubourg <lbedubourg@motion-twin.com>
 * @author   Kornel Lesiński <kornel@aardvarkmedia.co.uk>
 * @license  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
 * @version  SVN: $Id: CodeGenerator.php 605 2009-05-03 02:50:26Z kornel $
 * @link     http://phptal.org/
 */

require_once 'PHPTAL/Php/State.php';
require_once 'PHPTAL/Php/CodeWriter.php';

/**
 * @package PHPTAL
 * @subpackage Php
 */
class PHPTAL_Php_CodeGenerator
{
    public function __construct($function_name, $source_path, $enc, $mode, $cachepath)
    {
        $this->_functionName = $function_name;
        $this->_sourceFile = $source_path;
        $this->_state = new PHPTAL_Php_State();
        $this->_state->setEncoding($enc);
        $this->_state->setCacheFilesBaseName($cachepath);
        $this->_state->setOutputMode($mode);
    }


    public function generateCode(PHPTAL_Dom_Element $treeGen)
    {
        $codewriter = new PHPTAL_Php_CodeWriter($this->_state);

        $codewriter->doComment("\n*** DO NOT EDIT THIS FILE ***\n\nGenerated by PHPTAL from ".$this->_sourceFile." (edit that file instead)");
        $codewriter->doFunction($this->_functionName, '$tpl, $ctx');
        $codewriter->setFunctionPrefix($this->_functionName . "_");
        $codewriter->doSetVar('$_thistpl', '$tpl');
        $codewriter->doSetVar('$_translator', '$tpl->getTranslator()');
        $treeGen->generateCode($codewriter);
        $codewriter->doEnd();

        return $codewriter->getResult();
    }

    private $_functionName;
    private $_sourceFile;
    private $_state;
}

